package org.tigr.htc.server;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * This is a very simple servlet that is used to configure and monitor the
 * Log4j configuration file. When the servlet container initializes this
 * servlet, future changes to the log4j configuration file should get picked
 * up automatically without having to restart the web application of the
 * servlet container. The recommended configuration block for inclusion into
 * the application's web.xml file is given below: 
 * 
 * This class is copied almost verbatim from the example provided in the log4j
 * manual...
 * 
 * <servlet>
 *   <servlet-name>log4j-init</servlet-name>
 *   <servlet-class>org.tigr.htc.server.LoggingServlet</servlet-class>
 *
 *   <init-param>
 *     <param-name>log4j-config</param-name>
 *     <param-value>WEB-INF/classes/log4j.properties</param-value>
 *   </init-param>
 *
 *   <load-on-startup>1</load-on-startup>
 * </servlet>
 */

public class LoggingServlet extends HttpServlet {

	private static final long serialVersionUID = -6230236817459298399L;
	private static final String configParamName = "log4j-config";

	public void init() {
		String prefix = getServletContext().getRealPath("/");
		String file = getInitParameter(configParamName);
		// if the log4j-config parameter is not set, then there is no
		// point trying to configure the logging...
		if (file == null) {
			System.out.println("No log4j configuration file specified with the " +
					            configParamName + " parameter.");
		} else {
			PropertyConfigurator.configureAndWatch(prefix + file);
		}
	}

	public void doGet(HttpServletRequest req, HttpServletResponse res) {
		// Don't do anything here...
	}
}
